﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Data.SqlClient;
using IQKS.AWP.DataAccessLayer;

public partial class Admin_CandidateBriefMapping : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //GetCandidateBriefMapping();
            //GetCountryMaster();
            GetData();
            int count = gvCandidateBriefMapping.Rows.Count;
            if (count <= 0)
            {
                btnSave.Visible = false;
                btnCancel.Visible = false;
            }
            else
            {
                btnSave.Visible = true;
                btnCancel.Visible = true;
            }
        }
    }

    //private void GetCountryMaster()
    //{
    //    ddlCountryName.DataSource = new CountryMasterBAL().GetCountry();
    //    ddlCountryName.DataTextField = "CountryName";
    //    ddlCountryName.DataValueField = "CountryId";
    //    ddlCountryName.DataBind();
    //    ddlCountryName.Items.Insert(0, new ListItem("-- Select Country Title --", "0"));
    //}
    //private void GetCandidateBriefMapping()
    //{
    //    ddlBriefTitle.DataSource = GetCandidateBriefMappingDataSet();
    //    ddlBriefTitle.DataTextField = "BriefTitle";
    //    ddlBriefTitle.DataValueField = "BriefID";
    //    ddlBriefTitle.DataBind();
    //    ddlBriefTitle.Items.Insert(0, new ListItem("-- Select Brief Title --", "0"));
    //}

    private DataSet GetCandidateBriefMappingDataSet()
    {

        DataSet objDs = null;
        try
        {
            objDs = new CandidateBriefBAL().GetCandidateBriefMapping();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;

    }


    private DataSet GetDataSet()
    {

        DataSet objDs = null;
        try
        {
            objDs = new CandidateBriefBAL().GetCandidateBriefMappingbyCandidateDetails();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    private void GetData()
    {
        DataSet ds = GetDataSet();
        
        DataView objDv = new DataView();
        objDv = ds.Tables[0].DefaultView;
        //objDv.RowFilter = "(CountryId = " + ddlCountryName.SelectedValue.ToString() +")";
        gvCandidateBriefMapping.DataSource = objDv;
        gvCandidateBriefMapping.DataBind();

        //bjp added to show candiate whome country has not only one active brief
        gvBriefIssue.DataSource = ds.Tables[1];
        gvBriefIssue.DataBind();

    }
    protected void ddlBriefTitle_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            CandidateBriefBAL objCBBal;
            CandidateBriefBO objCBBO;
            Guid gudUserID = CommonUtility.GetLoggedOnUserId();
            string strReturnValue;

            objCBBal = new CandidateBriefBAL();
            objCBBO = new CandidateBriefBO();

            objCBBO.SetMethod = "INSERT";
            objCBBO.CandidateBriefID = 0;
            //objCBBO.BriefID = Convert.ToInt32(ddlBriefTitle.SelectedValue.Trim());
            objCBBO.BriefID = 0;

            string strPartnerID = "";
            string strCandidateID = "";
            int PartnerID = 0;
            int CandidateID = 0;
            foreach (GridViewRow row in gvCandidateBriefMapping.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkBox = (CheckBox)(row.Cells[0].FindControl("chkIsActive"));

                    if (chkBox.Checked)
                    {
                         PartnerID = Convert.ToInt32(row.Cells[5].Text);
                         CandidateID = Convert.ToInt32(row.Cells[1].Text);

                         if (PartnerID > 0)
                         {
                             strCandidateID = strCandidateID + "," + Convert.ToString(CandidateID) + "," + Convert.ToString(PartnerID);
                         }
                         else
                         {
                             strCandidateID = strCandidateID + "," + Convert.ToString(CandidateID);
                         }
                    }
                }
            }
            objCBBO.CandidateID = strCandidateID.Substring(1, strCandidateID.Length - 1);
            objCBBO.UserId = gudUserID;


            strReturnValue = (string)objCBBal.SetCandidateBriefDetail(objCBBO);
            if (strReturnValue == "SUCCESS")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Brief has been assigned successfully.');", true);
                //lblMessage.Text = "Brief has been assigned successfully.";
                //lblMessage.ForeColor = System.Drawing.Color.Green;

            }
            else if (strReturnValue == "EXIST")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Payment Invoice already exists.');", true);
                //lblMessage.Text = "Payment Invoice already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //  
                //lblMessage.ForeColor = System.Drawing.Color.Red; 
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                //lblMessage.ForeColor = System.Drawing.Color.Red;
            }
            //GetCandidateBriefMapping();
            GetData();

            int count = gvCandidateBriefMapping.Rows.Count;
            if (count <= 0)
            {
                btnSave.Visible = false;
                btnCancel.Visible = false;
            }
            else
            {
                btnSave.Visible = true;
                btnCancel.Visible = true;
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
    }
    protected void gvCandidateBriefMapping_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvCandidateBriefMapping_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCandidateBriefMapping.PageIndex = e.NewPageIndex;
        GetData();
    }


    protected void gvCandidateBriefMapping_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (GridViewRow row in gvCandidateBriefMapping.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkBox = (CheckBox)(row.Cells[0].FindControl("chkIsActive"));
                //if (chkBox.Checked)
                //{
                string Registrationno = Convert.ToString(row.Cells[2].Text);
                int PartnerID = Convert.ToInt32(row.Cells[5].Text);
                if (PartnerID > 0)
                {
                    DataSet ds = DataUtils.ExecuteDataset("Usp_GetCandidateCheckPartnerBrief", PartnerID);
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + Registrationno + "Your Partner Still Not reach this State');", true);
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect2", "alert('Your Partner Still Not reach this State'); window.location='" + Request.Url.ToString().Split('?').First().Replace(Request.Url.AbsolutePath, "")
                        //    + HttpContext.Current.Request.ApplicationPath + "Admin/CandidateBriefMapping.aspx';", true);
                        chkBox.Checked = false;
                        chkBox.Enabled = false;
                    }
                    else
                    {
                        chkBox.Checked = false;
                    }
                }
            }


        }
    }



    
}